<template>
  <div class="app-container">
    <div class="app-title">
      <span>收款户配置</span>
    </div>
    <!-- 按钮 -->
    <div class="filter-container">
      <div>
        <el-col style="text-align:right;margin-top: -8px;">
          <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="moreQuery">
            查询
          </el-button>
          <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="handleAdd">
            新增
          </el-button>
          <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="handleEdit">
            编辑
          </el-button>
          <el-button size="small" class="filter-item" type="primary" :loading="deleteLoading" @click="handleDelete">
            删除
          </el-button>
          <el-button size="small" class="filter-item" style="margin-left: 10px;margin-top: 6px; " type="primary"
            @click="outPutFile">
            模板导出
          </el-button>
          <el-button size="small" class="filter-item" style="margin-left: 10px;margin-top: 6px; " type="primary"
            @click="importFile">
            导入
          </el-button>
        </el-col>
      </div>
    </div>
    <el-dialog v-dialogDrag title="查询" :visible.sync="dialogQueryVisible" :close-on-click-modal="false" width="50%"
      style="padding:0px">
      <el-form :model="listQuery" label-position="right" label-width="100px" class="dialog-backgroundcolor"
        size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="收款人姓名：">
              <el-input v-model="listQuery.payeeAcctName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款人账户：">
              <el-input v-model="listQuery.payeeAcctNo" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款银行编码">
              <el-input v-model="listQuery.payeeAcctCode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款人银行：">
              <el-input v-model="listQuery.payeeAcctBankName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="text-align: right; margin-top: 1%">
            <el-button size="small" class="filter-item" style="margin-left: 10px;" type="primary" @click="handleFilter">
              查询
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 表格部分 -->
    <el-table ref="singalTable" v-loading="listLoading" :height="tableHeight" :data="list" :stripe="true" fit
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }">
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column type="index" align="center" width="50" label="序号" />
      <el-table-column label="系统显示名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.showName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payeeAcctName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人账户" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payeeAcctNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人银行编码" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payeeAcctCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款人银行" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payeeAcctBankName }}</span>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 新增/编辑页面部分 -->
    <el-dialog v-dialogDrag :close-on-click-modal="false" :title="dialogStatus" :visible.sync="dialogFormVisible"
      width="20%">
      <el-form ref="dataForm" class="dialog-backgroundcolor" :rules="dataFormRules" :model="formData"
        label-position="top" label-width="120px" size="small">
        <div class="formDeviation">
          <el-row>
            <el-col>
              <el-form-item label="系统显示名：" prop="showName" class="item-margin">
                <el-input v-model="formData.showName" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="收款人姓名：" prop="payeeAcctName" class="item-margin">
                <el-input v-model="formData.payeeAcctName" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item style="margin-top:10px" label="收款人账户：" prop="payeeAcctNo" class="item-margin">
                <el-input v-model="formData.payeeAcctNo" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item style="margin-top:10px" label="收款人银行编码：" prop="payeeAcctCode" class="item-margin">
                <el-input v-model="formData.payeeAcctCode" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item style="margin-top:10px" label="收款人银行：" prop="payeeAcctBankName" class="item-margin">
                <el-input v-model="formData.payeeAcctBankName" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col style="text-align: center; margin-top:20px; margin-bottom:8px">
            <el-button type="primary" :loading="saveLoading" @click="updateData()">
              保存
            </el-button>
            <el-button @click="dialogFormVisible = false">
              取消
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog v-dialogDrag :close-on-click-modal="false" title="导入" :visible.sync="dialogImportFormVisible"
      width="29%">
      <el-upload class="upload-demo" drag style="padding-left: 2px" action="#" list-type="multipart/form-data"
        accept=".xls,.xlsx" :show-file-list="false" :http-request="uploadExcel">
        <i class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip" />
      </el-upload>
    </el-dialog>
  </div>
</template>
<script>
import { getAccountInfo, saveAccount, updateAccount, deleteAccount, checkRepeat, outPutPayeeReportList, payeeImport } from '@/api/salaryConfigManage/payAccountConfig'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'Unit',
  components: { Pagination, tableEmpty },
  filters: {
  },
  data() {
    return {
      tableHeight: null,
      that: this,
      list: null,
      total: 0,
      listLoading: true,
      saveLoading: false,
      deleteLoading: false,
      dialogImportFormVisible: false,
      isEdit: false,
      listQuery: {
        page: 1,
        limit: 200
      },
      formData: {
        id: '',
        mofDivCode: '', // 区划
        year: '', // 年份
        showName: '',//系统显示名
        payeeAcctName: '', // 收款人姓名
        payeeAcctNo: '', // 收款人账户
        payeeAcctCode: '',
        payeeAcctBankName: '' // 收款人银行
      },
      dialogFormVisible: false,
      dialogStatus: '',
      dataFormRules: {
        showName: [
          { required: true, message: '请输入收款人姓名', trigger: 'blur' }
        ],
        payeeAcctName: [
          { required: true, message: '请输入收款人姓名', trigger: 'blur' }
        ],
        payeeAcctNo: [
          { required: true, message: '请输入收款人账户', trigger: 'blur' },
          {
            // pattern: /^([1-9]{1})(\d{14}|\d{15}|\d{16}|\d{17}|\d{18})$/,
            pattern: /^\d+-{0,1}\d+$/,
            message: '请输入正确的收款人账户',
            trigger: 'blur'
          }
        ],
        payeeAcctCode: [
          { required: true, message: '请输入收款人账户编码', trigger: 'blur' }
        ],
        payeeAcctBankName: [
          { required: true, message: '请输入收款人银行', trigger: 'blur' }
        ]
      },
      dialogQueryVisible: false
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 198
    },
    getList() {
      this.listLoading = true
      getAccountInfo(this.listQuery).then((response) => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    moreQuery() {
      this.listQuery = Object.assign({}, this.listQuery, '')
      this.dialogQueryVisible = true
    },
    handleFilter() {
      this.dialogQueryVisible = false
      this.listQuery.page = 1
      this.getList()
    },
    // 新增
    handleAdd() {
      this.formData = Object.assign({}, '')
      this.formData.id = ''
      this.dialogStatus = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑
    handleEdit() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选择要编辑的收款账户',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.dialogStatus = '编辑'
      this.dialogFormVisible = true
      this.formData = Object.assign({}, this.formData, selection[0])
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 保存
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          const tempData = Object.assign({}, this.formData)
          const param = {
            'payeeAcctNo': this.formData.payeeAcctNo,
            'payeeAcctCode': this.formData.payeeAcctCode,
            'id': this.formData.id
          }
          // checkRepeat(param).then((response) => {
          //   if (response.data !== '') {
          //     this.$message({
          //       showClose: true,
          //       message: response.data,
          //       type: 'warning',
          //       duration: 4000
          //     })
          //     this.saveLoading = false
          //   } else {
          if (tempData.id === '') { // 新增
            saveAccount(tempData).then((response) => {
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: '保存成功',
                type: 'success',
                duration: 4000
              })
              this.saveLoading = false
              this.handleFilter()
            }).catch(() => {
              this.saveLoading = false
            })
          } else { // 修改
            updateAccount(tempData).then((response) => {
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: '编辑成功',
                type: 'success',
                duration: 4000
              })
              this.saveLoading = false
              this.handleFilter()
            }).catch(() => {
              this.saveLoading = false
            })
          }
          // }
          //   }).catch(() => {
          //     this.saveLoading = false
          //   })
        }
      })
    },
    // 删除
    handleDelete() {
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要删除的数据',
          type: 'warning',
          duration: 4000
        })
        return
      }
      this.deleteLoading = true
      const operate = []
      selection.forEach(item => {
        operate.push(item.id)
      })
      this.$confirm('确定删除吗?', '提示', {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAccount(operate).then((response) => {
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success',
            duration: 4000
          })
          this.handleFilter()
          this.deleteLoading = false
        }).catch(() => {
          this.deleteLoading = false
        })
      }).catch(() => {
        this.deleteLoading = false
      })
    },
    // 导出
    outPutFile() {
      outPutPayeeReportList().then((response) => {
        const link = document.createElement("a");
        const objectUrl = URL.createObjectURL(response);
        link.href = objectUrl;
        link.download = "收款户配置.xls";
        link.click();
        URL.revokeObjectURL(objectUrl);
      });
    },
    // 上传文件
    uploadExcel(item) {
      const fileObj = item.file;
      const salaryMonth = this.selectMonth;
      // FormData 对象
      const form = new FormData();
      // 文件对象
      form.append("file", fileObj);
      form.append("salaryMonth", salaryMonth);
      payeeImport(form)
        .then((response) => {
          this.$message({
            showClose: true,
            message: "导入成功",
            type: "success",
            duration: 4000,
          });
          this.dialogImportFormVisible = false;
        })
        .then(() => {
          this.getList();
        });
    },
    // 导入
    importFile() {
      this.dialogImportFormVisible = true;
    }
  }
}
</script>
<style lang="scss" scoped>
.formDeviation {
  margin-left: 10%;

  .el-form-item__error {
    padding-top: 5%;
  }
}

.redstar.el-form-item__error {
  margin-top: 35px !important;
  left: -96px !important;
}
</style>
